/*
 * ExportXLS.java
 *
 * Created on September 15, 2005, 8:00 AM
 */

package Excel;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import DataAccess.*;


/**
 *
 * @author afortun
 * @version
 */
public class ExportXLS extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        /* TODO output your page here
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet ExportXLS</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet ExportXLS at " + request.getContextPath () + "</h1>");
        out.println("</body>");
        out.println("</html>");
         */
        out.close();
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        //processRequest(request, response);
        
        String sql = request.getParameter("query");
        String titolo = request.getParameter("titolo");
        String driverstring = request.getParameter("driverstring");
        String connectionstring = request.getParameter("connectionstring");
        // step 1: creation of a document-object
        //Document document = new Document();
        
        String nome_file = titolo.replace(" ", "_");
        
        //response.setContentType("application/x-download");
        response.addHeader("Content-Disposition", "attachment; filename=\""+ nome_file.replace(",", "_") + ".xls\"");
        //response.setContentType("application/pdf");
        
        DataAccess.Actions actions = new DataAccess.Actions();
        actions.setDriverString(driverstring);
        actions.setConnectionString(connectionstring);
        
        PrintWriter out = response.getWriter();

        out.write(actions.getExcelListFromQuery(sql));
        
        out.close();
        
        
        
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
